package com.wmobile.utils;

import java.util.List;

import android.app.Dialog;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.KeyEvent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

import com.wellsoft.mobileoa.R;
import com.wellsoft.mobileoa.utils.BaseActivity;

public class PopReasonDialog <WSOA_ActionData> extends Dialog {
	BaseActivity baseActivity;
	String title;
	String opinion;
	Handler mHandler;
	int dialogResult;
	public static int DialogResultOK = 1;
	public static int DialogResultCANCEL = 0;

	TextView titleTv;
	ListView dataListLv;
	Button confirmBtn;
	Button cancelBtn;
	EditText opinionEt;
	PopListDialogAdapter<WSOA_ActionData> dataListAdapter;

	public String getOpinion() {
		return opinion;
	}

	public int getDialogResult() {
		return dialogResult;
	}

	public PopReasonDialog(BaseActivity baseActivity, String title, String opinion) {
		super(baseActivity, R.style.popDialogTheme);
		this.title = title;
		this.baseActivity = baseActivity;
		this.opinion = opinion;

		onCreate();
	}

	public void onCreate() {
		setContentView(R.layout.popreasondialog);

		opinionEt = (EditText) findViewById(R.id.opinionet);
		opinionEt.setText(opinion);

		titleTv = (TextView) findViewById(R.id.titletv);
		titleTv.setText(title);

		confirmBtn = (Button) findViewById(R.id.confirmbtn);
		confirmBtn.setOnClickListener(onConfirmListener);
		confirmBtn.setVisibility(View.VISIBLE);

		cancelBtn = (Button) findViewById(R.id.cancelbtn);
		cancelBtn.setOnClickListener(onCancelListener);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) {
			onCancelListener.onClick(null);
		}

		return super.onKeyDown(keyCode, event);
	}

	View.OnClickListener onConfirmListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			endDialog(DialogResultOK);
		}
	};

	View.OnClickListener onCancelListener = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			endDialog(DialogResultCANCEL);
		}
	};

	public void endDialog(int result) {
		opinion = opinionEt.getText().toString();
		dismiss();
		dialogResult = result;
		Message m = mHandler.obtainMessage();
		mHandler.sendMessage(m);
	}

	public int showDialog() {
		mHandler = new Handler() {
			@Override
			public void handleMessage(Message mesg) {
				throw new RuntimeException();
			}
		};
		super.show();
		try {
			Looper.getMainLooper();
			Looper.loop();
		} catch (Exception e2) {
			e2.printStackTrace();
		}
		return dialogResult;
	}

}